package com.techsoul.module.pms.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.common.core.exception.ParamException;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.bean.BeanUtils;

import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.core.util.id.IdWorker;

import com.techsoul.common.security.service.TokenService;

import com.techsoul.module.pms.mapper.*;
import com.techsoul.module.pms.pojo.entity.*;

import com.techsoul.module.pms.pojo.param.TbWmsMaterialReceiptNotExistSaveParam;
import com.techsoul.module.pms.pojo.param.TbWmsMaterialReceiptNotExistSearchParam;
import com.techsoul.module.pms.pojo.vo.*;

import com.techsoul.module.pms.service.TbWmsMaterialReceiptNotExistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 出库明细 Service实现
 *
 * @author liuen
 * @date 2022-01-25
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class TbWmsMaterialReceiptNotExistServiceImpl implements TbWmsMaterialReceiptNotExistService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TbWmsMaterialReceiptNotExistMapper mapper;
    @Autowired
    private TokenService tokenService;

    @Override
    public String insert(TbWmsMaterialReceiptNotExistSaveParam params) {
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        TbWmsMaterialReceiptNotExist entity = new TbWmsMaterialReceiptNotExist();
        BeanUtils.copyProperties(params, entity);
        entity.setId(idWorker.nextId() + "").preInsert(sysUser.getId());
        this.mapper.insert(entity);
        return entity.getId();
    }

    @Override
    public Integer delete(String id) {
        return this.mapper.deleteById(id);
    }

    @Override
    public Integer batchDelete(List<String> idList) {
        if (ListUtils.isNotEmpty(idList)) {
            return this.mapper.deleteBatchIds(idList);
        } else {
            return 0;
        }
    }

    @Override
    public Integer update(TbWmsMaterialReceiptNotExistSaveParam params) {
        TbWmsMaterialReceiptNotExist entityFromDb = this.mapper.selectById(params.getId());
        if (entityFromDb == null) {
            throw new ParamException("id=" + params.getId());
        }
        UserVo sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        BeanUtils.copyProperties(params, entityFromDb);
        entityFromDb.preUpdate(sysUser.getId());
        return this.mapper.updateById(entityFromDb);
    }

    @Override
    public TbWmsMaterialReceiptNotExistVo findById(String id) {
        return mapper.findById(id);
    }

    @Override
    public List<TbWmsMaterialReceiptNotExistVo> findAll(TbWmsMaterialReceiptNotExistSearchParam params) {
        return mapper.findAll(params);
    }

    @Override
    public IPage<TbWmsMaterialReceiptNotExistVo> findAll(TbWmsMaterialReceiptNotExistSearchParam params, Integer current, Integer pageSize) {
        return this.mapper.findAll(new Page<>(current, pageSize), params);
    }
}
